home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / zendisk2.zip / LST11-32.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  3KB  |  107 lines

  1. ;
  2. ; *** Listing 11-32 ***
  3. ;
  4. ; Compares two arrays of 16-bit signed values in order to
  5. ; find the first point at which the arrays cross, using
  6. ; non-string instructions.
  7. ;
  8.     jmp    Skip
  9. ;
  10. ; The two arrays that we'll compare.
  11. ;
  12. ARRAY_LENGTH    equ    200
  13. ;
  14. Array1    label    byte
  15. TEMP=-100
  16.     rept    ARRAY_LENGTH
  17.     dw    TEMP
  18. TEMP=TEMP+1
  19.     endm
  20. ;
  21. Array2    label    byte
  22. TEMP=100
  23.     rept    ARRAY_LENGTH
  24.     dw    TEMP
  25. TEMP=TEMP-1
  26.     endm
  27. ;
  28. ; Compares two buffers to find the first point at which they
  29. ; cross. Points at which the arrays become equal are
  30. ; considered to be crossing points.
  31. ;
  32. ; Input:
  33. ;    CX = length of arrays in words (they must be of
  34. ;        equal length)
  35. ;    DS:SI = start of first array
  36. ;    ES:DI = start of second array
  37. ;
  38. ; Output:
  39. ;    DS:SI = pointer to crossing point in first array,
  40. ;        or SI=0 if there is no crossing point
  41. ;    ES:DI = pointer to crossing point in second array,
  42. ;        or DI=0 if there is no crossing point
  43. ;
  44. ; Registers altered: BX, CX, DX, SI, DI
  45. ;
  46. ; Note: Does not handle arrays that are longer than 64K
  47. ;    bytes or cross segment boundaries.
  48. ;
  49. FindCrossing:
  50.     jcxz    FindCrossingNotFound
  51.             ;if there's nothing to compare, we
  52.             ; certainly can't find a crossing
  53.     mov    dx,2    ;amount we'll add to the pointer
  54.             ; registers after each comparison,
  55.             ; kept in a register for speed
  56.     mov    bx,[si]    ;compare the first two points to
  57.     cmp    bx,es:[di] ; make sure that the first array
  58.             ; doesn't start out below the second
  59.             ; array
  60.     pushf        ;remember the original relationship
  61.             ; of the arrays, so we can put the
  62.             ; pointers back at the end (can't
  63.             ; use LAHF because it doesn't save
  64.             ; the Overflow flag)
  65.     jnl    FindCrossingLoop ;the first array is above
  66.                 ; the second array
  67.     xchg    si,di    ;swap the array pointers so that
  68.             ; SI points to the initially-
  69.             ; greater array
  70. FindCrossingLoop:
  71.     mov    bx,[si]        ;compare the next element in
  72.     cmp    bx,es:[di]    ; each array
  73.     jng    FindCrossingFound ;if SI doesn't point to a
  74.                 ; greater value, we've found
  75.                 ; the first crossing
  76.     add    si,dx        ;point to the next element
  77.     add    di,dx        ; in each array
  78.     loop    FindCrossingLoop ;check the next element in
  79.                 ; each array
  80. FindCrossingNotFound:
  81.     popf        ;clear the flags we pushed earlier
  82.     sub    si,si    ;return 0 pointers to indicate that
  83.     mov    di,si    ; no crossing was found
  84.     ret
  85. FindCrossingFound:
  86.     popf        ;get back the original relationship
  87.             ; of the arrays
  88.     jnl    FindCrossingDone
  89.             ;SI pointed to the initially-
  90.             ; greater array, so we're all set
  91.     xchg    si,di    ;SI pointed to the initially-
  92.             ; less array, so swap SI and DI to
  93.             ; undo our earlier swap
  94. FindCrossingDone:
  95.     ret
  96. ;
  97. Skip:
  98.     call    ZTimerOn
  99.     mov    si,offset Array1 ;point to first array
  100.     mov    di,seg Array2
  101.     mov    es,di
  102.     mov    di,offset Array2 ;point to second array
  103.     mov    cx,ARRAY_LENGTH    ;length to compare
  104.     call    FindCrossing    ;find the first crossing, if
  105.                 ; any
  106.     call    ZTimerOff
  107.